<template>
  <div class="inventoryCenter">
    <c-title :hide="false" text="库存中心"></c-title>
    <van-search v-model="keyword" shape="round" placeholder="搜索商品" @search="getData" />
    <div class="shopList">
      <div class="lis" v-for="(item, index) in info" :key="index" @click="toRecord(item)">
        <div class="lis_l">
          <img v-if="item.goods" :src="item.goods.thumb" alt="" />
        </div>
        <div class="lis_r">
          <div class="title" v-if="item.goods">{{ item.goods.title }}</div>
          <div class="lis_r_b">
            <div class="lis_r_b_l">
              <div class="inventory">库存：{{ item.stock }}</div>
            </div>
          </div>
        </div>
      </div>
      <div v-if="page >= total_page" class="txtNone">没有更多</div>
    </div>
  </div>
</template>
<script>
import { scrollMixin } from "../../utils/mixin";
export default {
  mixins: [scrollMixin], //加载更多
  data() {
    return {
      keyword: "",
      info: [],
      page: 1,
      isLoadMore: true,
      total_page: 0
    };
  },
  activated() {
    this.init();
    this.getData();
  },
  methods: {
    init() {
      this.info = [];
      this.page = 1;
      this.isLoadMore = true;
      this.total_page = 0;
    },
    toRecord(item) {
      this.$router.push(this.fun.getUrl("inventoryDetail", { id: item.goods_id }));
    },
    getData() {
      $http
        .get(
          "plugin.gradevin-stock.frontend.controllers.index.stock-center",
          {
            keyword: this.keyword,
            page: this.page
          },
          "."
        )
        .then(
          response => {
            if (response.result === 1) {
              this.isLoadMore = true;
              this.total_page = response.data.list.last_page;
              if (!this.total_page) {
                this.total_page = 0;
              }
              this.info = response.data.list.data;
            } else {
              this.$toast(response.msg);
            }
          },
          function(response) {
            console.log(response);
          }
        );
    },
    getMoreData() {
      this.isLoadMore = false; // 防止多次请求分页数据
      if (this.page >= this.total_page) {
        return;
      } else {
        this.page += 1;
        $http
          .get("plugin.gradevin-stock.frontend.controllers.index.stock-center", {
            keyword: this.keyword,
            page: this.page
          })
          .then(response => {
            this.isLoadMore = true;
            if (response.result === 1) {
              var nextPageData = response.data.list.data;
              this.info = this.info.concat(nextPageData);
            } else {
              this.page = this.page - 1;
              this.isLoadMore = false;
              this.$toast(r);
            }
          });
      }
    }
  }
};
</script>
<style lang="scss" scoped>
.inventoryCenter {
  .shopList {
    padding: 0 0.75rem;
    box-sizing: border-box;
    padding-bottom: 5rem;
    .lis {
      display: flex;
      padding: 0.75rem;
      border-radius: 0.5rem;
      margin-top: 0.63rem;
      background: #fff;
      .lis_l {
        flex-shrink: 0;
        width: 5.63rem;
        height: 5.63rem;
        border-radius: 0.5rem;
        margin-right: 0.63rem;
        img {
          width: 5.63rem;
          height: 5.63rem;
          border-radius: 0.5rem;
        }
      }
      .lis_r {
        flex: 1;
        display: flex;
        flex-direction: column;
        .title {
          text-align: left;
          font-weight: 500;
          font-size: 0.88rem;
        }
        .lis_r_b {
          flex: 1;
          display: flex;
          justify-content: space-between;
          align-items: flex-end;
          .lis_r_b_l {
            height: 100%;
            display: flex;
            flex-direction: column;
            align-items: flex-start;
            justify-content: space-between;
            flex: 1;
            text-align: left;
            font-size: 0.81rem;
            color: #6e6e79;
            .inventory {
              line-height: 1.25rem;
              margin-top: 0.7rem;
            }
            .myinventory {
              line-height: 1.25rem;
            }
          }
          .lis_r_b_r {
            width: 1.25rem;
            height: 1.25rem;
            background: #f15353;
            border-radius: 50%;
            .icon-icon_add {
              color: #fff;
              font-size: 0.65rem;
            }
          }
        }
      }
    }
  }
}

.txtNone {
  text-align: center;
  color: #aaaab3;
  margin-top: 1rem;
}
</style>
